<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解构赋值</title>
    <script>

        //数组解构
        const arr = [
            ["Amy",18,"woman"],
            ["Mike",20,"man"]
        ]

        /*
        把数组内容拆开赋值给所需对象，灵活便捷
        */

        let [[name,age,gender],obj] = arr
        console.log(name,age,gender);   //Amy 18 woman
        console.log(obj);   //Mike对象

        /*
        快速交换两个变量的值
        */

        ;[arr[0],arr[1]] = [arr[1],arr[0]]
        console.log(arr)   //交换后Mike对象在前，Amy对象在后


        //对象解构
        const obj2 = {
            name1:"Sary",
            age1:18,
            gender1:"female"
        }

        let {name1,age1,gender1} = obj2
        console.log(name1,age1,gender1);    //Sary 18 female

        /*
        若在对象中找不到该属性，则输出undefined
        */
        let{address} = obj2
        console.log(address);   //undefined

        /*
        可以给查找属性指定一个值，查找不到时才输出指定值
        */
        let{name1:a,age1:b,gender1:c="male",address1:d="home"} = obj2
        console.log(a,b,c,d);    //Sary 18 female home
    </script>
</head>
<body>
    
</body>
</html>